
/******************************************************************************
 * jPoint dubug Object Class by Samir Sijercic 4/24/2009
 ******************************************************************************/
//Logging functionality
(function($) {
	$.extend({
        // boolean status whether debugging is enabled
        _debug$: null,
		// default debug level higher the number more data shown
		_level$:0,
		// log line number
		_logline$:1,
        // method for getting and setting debug status
        debug: function (onoff) {
            var value = ($._debug$ == true ? true : false);
            $._debug$ = (onoff ? true : false);
            return value;
        },
		loglevel: function(lvl) {
			var value = ($._level$);
            $._level$ = (lvl);
            return value;
		},
		logline: function(line){
			var value = ($._logline$);
			$._logline$ = line;
			return value;
		},
        // method for logging an object or message
        log: function (message, level) {
        if (level == null || level < 1)
        		level = 1;
        	if ($._debug$ == true && $._level$ >= level)
        	{
        		window.console.debug(message, level);
            }
        },
        //One place to set all function logging
        logfunction: function (message)
        {
        	$.log(message, 2);
        },
        //One place to set all variable logging
        loginfo: function (message)
        {
        	$.log(message, 3);
        }
    });
    
    // jQuery object extension methods
    $.fn.extend({
        /* method for logging all jQuery items */
        log: function (message, level) {
        	if (level == null || level < 1)
        		level = 1;
        	if ($._debug$ == true && $._level$ >= level) {
                return this.each(function () {
                	if (typeof message !== "undefined" && message != null)
                	{
                	   $.log(message, level);
                    }
                    $.log(this, level);
                });
            }
        }
	});
    
    // determine default enable status
    $(document).ready(function () {
        var req = $("html").attr("debug");
        if (req == "true" || req == "false")
            $.debug(req == "true" ? true : false);
            
        req = $("html").attr("level");
        if (req == "0" || req == "1" || req == "2" || req == "3" || req == "4" || req == "5")
            $.loglevel(req);
    });

    // minimum Firebug emulation (see http://getfirebug.com/firebug/firebugx.js)
    $(document).ready(function () {
    	if (typeof window.console === "undefined") {
            /* minimum conversion of arbitrary object to text representation */
            function object2text (obj, lvl) {
            	//Each line starts with line number
            	var text = $._logline$ + ":";
            	//Increase line number for next line
            	$._logline$ = $._logline$ + 1;
				//print identation which is level dependent
				for(var i=0; i<lvl; i++)
				{
					text += "&nbsp;&nbsp;";
				}
				//identify type of object
                if (typeof obj === "undefined")
                    text += "[undefined]";
                else if (typeof obj === "boolean")
                    text += (obj ? "true" : "false");
                else if (typeof obj === "number")
                    text += "" + obj;
                else if (typeof obj === "string")
                    text += obj;
                else if (typeof obj === "function")
                    text += obj;
                else if (typeof obj === "object") {
                    if (typeof obj.nodeType !== "undefined") {
                        if (obj.nodeType == 1) { /* W3C DOM element node */
                            text += '&lt;';
                            text += obj.nodeName.toLowerCase();
                            for (var i = 0; i < obj.attributes.length; i++)
                                text += ' ' + obj.attributes[i].nodeName.toLowerCase() +
                                        '="' + obj.attributes[i].nodeValue + '"';
                            text += '&gt;';
                        }
                        else if (obj.nodeType == 2) /* W3C DOM attribute node */
                            text += obj.nodeName + '="' + obj.nodeValue;
                        else if (obj.nodeType == 3) /* W3C DOM text node */
                            text += obj.nodeValue;
                    }
                    else if (typeof obj.toJSONString !== "undefined")
                        text += obj.toJSONString();
                    else if (typeof obj.toString !== "undefined")
                        text += obj.toString();
                }
                if (text == null)
                    text += "[unknown]";
                return text;
            };

            // create the custom console <div> node
            $(document).ready(function () {
                $("body")
                    .append('<div id="jQueryDebug"><ol></ol></div>');
                $("#jQueryDebug")
                    .css("display", "none")
                    .css("fontFamily", "monospace")
                    .css("backgroundColor", "#ffffff")
                    .css("color", "#000000")
                    .css("padding", "10px 0px 10px 0px")
                    .css("border", "4px solid #666699")
                    .css("margin", "4px 4px 4px 4px");
                $("#jQueryDebug ol")
                    .css("margin", "0px 0px 0px 0px")
                    .css("paddingRight", "20px");
            });

            // attach a logging function to each of the usual Firebug methods
            var names = [
                "log", "debug", "info", "warn", "error", "assert",
                "dir", "dirxml", "group", "groupEnd", "time", "timeEnd",
                "count", "trace", "profile", "profileEnd"
            ];
            var logfunc = function(msg, lvl) {
            	$('#jQueryDebug')
                    .css("display", $._debug$ == true ? "block" : "none");
                $('#jQueryDebug ol')
                    .append('<li>' + object2text(msg, lvl) + '</li>').css;
                $("#jQueryDebug ol li")
                    .css("borderBottom", "1px solid #cccccc")
                    .css("padding", "1px 10px 1px 10px");
            };
            window.console = {};
            for (var i = 0; i < names.length; i++)
                window.console[names[i]] = logfunc;

            //indicate that we are the one who is providing the Firebug interface
            window.console["jQueryDebug"] = true;
        }
    });
})(jQuery);
